package model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import lucene.PaperSearcher;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.ScoreDoc;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

import data.TestUserSelection;
import data.TrainTestPaperRetrieval;

public class BuildAuthorModel {
	private String user;
	private Connection conn;

	private HashSet<String> tags = new HashSet<String>();
	private HashSet<String> holdoutPapers;;
	private HashSet<String> trainPapers;
	private IndexReader generalReader;
	private PaperSearcher generalSearcher;
	private HashSet<String> tagAuthors = new HashSet<String>();
	
	public BuildAuthorModel(String u, Connection c, IndexReader gReader, PaperSearcher gSearcher) throws Exception{
		this.user = u;
		this.conn = c;
		this.holdoutPapers = TrainTestPaperRetrieval.retrieveTest(user);
		this.trainPapers = TrainTestPaperRetrieval.retrieveTrain(user);
		this.generalReader = gReader;
		this.generalSearcher = gSearcher;
		initTags();
		initTagAuthors();
	}
	
	private void initTagAuthors() throws Exception{
		for(String paper : trainPapers) {
			HashSet<String> authors = generalSearcher.retrieveAuthorsByPid(paper);
			if(authors == null){
				System.err.println("paper\t" + paper + "\tauthors null");
				continue;
			}
			tagAuthors.addAll(authors);
		}		
	}
	
	private void initTags() throws Exception {
		String path = "D:\\CityU\\project\\bibsonomy\\experiment\\" + user
				+ "\\tagProb.txt";
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String line = null;
		while ((line = reader.readLine()) != null) {
			line = line.trim();
			String[] tokens = line.split("\t");
			String tag = tokens[0];
			tags.add(tag);			
		}

	}		
	
	private HashSet<String> retrieveUserPapersByTag(String tag, Statement stmt,
			ResultSet rs) throws Exception {
		HashSet<String> list = new HashSet<String>();

		if (tag.indexOf("\\") != -1)
			return list;
		tag = tag.replaceAll("'", "''");
		//tag = tag.replaceAll("\\", "\\\\");
		rs = (ResultSet) stmt
				.executeQuery("SELECT content_id FROM TAS_NEW where user = '"
						+ user + "' and " + "tag = '" + tag + "'");
		//HashSet<String> papers = new HashSet<String>();
		while (rs.next()) {
			String content_id = rs.getString("content_id");

			if (!trainPapers.contains(content_id))
				continue;

			//int doc = generalSearcher.retrieveDocByPid(content_id);
			list.add(content_id);
		}
		return list;
	}
	
	private HashSet<String> retrieveAuthorTagPapers(ScoreDoc[] docs, HashSet<String> tagPaperList) throws Exception{
		HashSet<String> list = new HashSet<String>();
		for(ScoreDoc doc : docs) {
			String pid = generalReader.document(doc.doc).get("content_id");
			if (tagPaperList.contains(pid))
				list.add(pid);
		}
		return list;
	}
	
	public void build() throws Exception{
		Statement stmt = null;
		ResultSet rs = null;
		String path = "D:\\CityU\\project\\bibsonomy\\experiment\\" + user
				+ "\\tag_author_map.txt";
		BufferedWriter writer = new BufferedWriter(new FileWriter(path));
		
		try{
			stmt = (Statement) conn.createStatement();
			HashSet<String> tagPaperList = null;
			HashSet<String> overlapList = null;
			for (String tag : tags) {
				tagPaperList = retrieveUserPapersByTag(tag, stmt, rs);
				for(String author : tagAuthors) {
					ScoreDoc[] authorPapers = generalSearcher.retrieveResultsByAuthorId(author);
					overlapList = retrieveAuthorTagPapers(authorPapers, tagPaperList);
					//HashSet<String> overlapSet = overlap(authorPaperList,tagPaperList);
					double up = overlapList.size() + 0.0;
					double down = tagPaperList.size() + 0.0;
					Double score = up / down;
					if(score.isNaN() ||  score == 0) {
						continue;
					}
					writer.write(tag + "\t" + author + "\t" + score);
					writer.newLine();
				}
				
			}
									
		} finally{			
			writer.flush();
			writer.close();
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException sqlEx) {
				} // ignore
				rs = null;
			}

			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException sqlEx) {
				} // ignore
				stmt = null;
			}
		
		}
		
	}
	
	private HashSet<String> overlap(HashSet<String> set1, HashSet<String> set2) {
		HashSet<String> set = new HashSet<String>();
		for(String str : set2) {
			if(set1.contains(str))
				set.add(str);
		}
		return set;
	}
		
	
	public HashSet<String> getTagAuthors() {
		return tagAuthors;
	}
		

	public HashSet<String> getTags() {
		return tags;
	}
	
	public static HashSet<String> doneCases() throws Exception{
		String path = "D:\\CityU\\project\\bibsonomy\\doneCases.txt";
		HashSet<String> cases = new HashSet<String>();
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String line = null;
		while((line = reader.readLine()) != null) {
			line = line.trim();
			String[] tokens = line.split("\t");
			String doneCase = tokens[1];
			cases.add(doneCase);
		}
		return cases;
	}

	public static void main(String[] args) throws Exception{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		String url = "jdbc:mysql://localhost:3306/" + "bibsonomy";
		String dbuser = "root";
		String dbpass = "123456";
		Connection con = (Connection) DriverManager.getConnection(url, dbuser,
				dbpass);
		List<String> users = TestUserSelection.retrieveTestUsers();
		File indexDir = new File("D:\\CityU\\project\\bibsonomy\\"
				+ "\\BibIndex\\");
		PaperSearcher gSearcher = new PaperSearcher(indexDir);
		IndexReader gReader = gSearcher.getReader();
		HashSet<String> doneCases = doneCases();
		for (String user : users) {
			if(doneCases.contains(user))
				continue;
            BuildAuthorModel app= new BuildAuthorModel(user,con,gReader,gSearcher); 
			System.out.println("build author model test user\t" + user + "\ttags\t" + app.getTags().size() + "\ttagAuthors\t" + app.getTagAuthors().size());
			app.build();
		}
		
	}

}
